<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index()
    {
        $this->display();
    }
    //验证码
//    public function verify(){
//        $config =    array(
//            'fontSize'    =>    50,    // 验证码字体大小
//            'length'      =>    3,     // 验证码位数
//            'useNoise'    =>    false, // 关闭验证码杂点
//            'fontttf' => '1.ttf',
//            'useImgBg' => false,
//            //'useZh' => true,
//        );
//        $Verify = new \Think\Verify($config);
//        $Verify->entry();
//    }
    public function checkLogin(){
        //$verify = new \Think\Verify();
//        if(!$verify->check($_POST['verify']))
//            $this->error('验证码错误');
        $admin=M('admins');
        $map   =   array();
        $map['adminname']	= $_POST['adminname'];
        $map['pass']	= $_POST['pass'];
        $res=$admin->where($map)->select();

//        print_r($res);
//        echo 123;
//        exit(123);

        if($res==null){
            $this->error('请重新输入');
        }else {
            session("flag","logok");
            redirect('users');
        }
    }

//*****************************显示信息*********************************************
//显示用户
    public function users(){
        $usersModel=M('users');
        $users=$usersModel->order('id desc')->select();
        $this->users=$users;
        $this->display();
    }
    //显示书籍列表
    public function books(){
        $booksModel=M('books');
//        $list = $booksModel->join('RIGHT JOIN signs ON books.sid = signs.sid' );
//        $stu=$list->join('RIGHT JOIN class ON class.cid = signs.cid' );
//        $total=$stu->order ("bid asc") ->select();
        $books=$booksModel->order('bid desc')->select();
        $this->booklist=$books;
        $this->display();
    }

    //显示类别列表
    public function classlist(){
        $classModel=M('class');
        $class=$classModel->order('cid desc')->select();
        $this->classlist=$class;
        $this->display();
    }
    //显示标签列表
    public function signlist(){
        $signsModel=M('signs');
        $signs=$signsModel->order('sid asc')->select();
        $this->signlist=$signs;
        $this->display();
    }
//*****************************删除信息*********************************************
//删除用户
    public function deluser($id){
        $user= M('users');
        if($user->delete($id)) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //删除书籍
    public function delBooks($bid){
        $book= M('books');
        if($book->delete($bid)) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //删除类别
    public function delclass($cid){
//       var_dump($_REQUEST); die("test");
//        $signs = $class->join('RIGHT JOIN signs ON class.cid = signs.cid' );
//        $sql=$signs->where(" cid = '$cid'")->select();
//       $sql="select cid from class,signs where cid=$cid;";
        $class= M('class');
        $sign= M('signs');
        $book= M('books');
        if($class->delete($cid) && $sign->delete($cid) && $book->delete($cid)) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //删除标签
    public function delsign($sid){
        $sign= M('signs');
        if($sign->delete($sid)) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //*****************************修改信息*********************************************
    //修改书籍信息
    public function editBooks($bid)
    {
        $bookTable = M('books');
        $class=M('class');
        $signs=M('signs');
        $res = $bookTable->where ("bid=$bid") ->find();
        $this->classlist=$class->order('cid asc')->select();
        $this->signs=$signs->order('sid asc')->select();
        $this->books = $res;
        $this->display();
    }

    public function updBooks($bid)
    {

        if($_FILES['pic']['error']==0){
            $logoinfo=$this->upload();
            $pname=$logoinfo['pic']['savename'];
            $image=new \Think\Image();
            $image->open("Public/Uploads/image/$pname")->thumb(150,150)
                ->text('Appstore','Public/fonts/arial.ttf',10,'#ffffff',9);
            $pname="$pname";
        }else{
            $num=rand(1,4);
            $pname="default{$num}.jpg";
        }

        $books = D("books");
        if ($books->create()) {
            $books->pic = $pname;
            $books->issuetime=date('Y-m-d H:i:s');
            if (false !== $books->where("bid=$bid")->save()) {
                //echo $user->getLastSql();
                $this->success('修改成功！', 'books');
            }
            else
            {         $this->error('数据错误');           }
        } else {
            $this->error($books->getError());
        }
    }

    public function upload(){

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Uploads/image/'; // 设置附件上传根目录
        //$upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->autoSub=false;
        $info   =   $upload->upload();  // 上传文件
        if(!$info) {
            $this->error($upload->getError());
        }
        else{
            //$this->success('上传成功！');
            return $info;
        }
    }
    //*****************************添加信息*********************************************
    //添加图书
    public function addBooks(){
        $class=M('class');
        $signs=M('signs');
       // $cid = $_REQUEST['cid'];
        $this->class=$class->order('cid asc')->select();
        $this->signs=$signs->order('sid asc')->select();

        //[{cid,cname,sid,sname},{cid,cname,sid,sname}……]
        $this->display();
    }
    public function addB(){
        if($_FILES['pic']['error']==0){
            $photoinfo=$this->upload();
            $pname=$photoinfo['pic']['savename'];
            $image=new \Think\Image();
            $image->open("Public/Uploads/image/$pname")->thumb(150,150)
                ->text('Appstore','Public/fonts/arial.ttf',10,'#ffffff',9);
            $pname="$pname";
        }else{
            $num=rand(1,4);
            $pname="default{$num}.jpg";
        }

        $books = D("books");
        if ($books->create()) {
            $books->pic = $pname;
            $books->cid = $_REQUEST['cid'];
            $books->sid = $_REQUEST['sid'];
            if (false !== $books->add()) {
                //echo $user->getLastSql();
                $this->success('数据写入成功！', 'books');
            }
            else
            {         $this->error('数据错误');           }
        } else {
            $this->error($books->getError());
        }

    }
//添加类别
public function addClass(){
    $addclass = M("class");
    $data["class"] = $_POST["class"];
    if ($lastInsId = $addclass->add($data)) {
        $this->error('数据写入成功！');
    } else {
        $this->error('数据写入错误！');
    }
    }
    //添加标签
    public function addsign(){
        $class=M('class');
        $signs=$class->order('cid asc')->select();
        $this->classlist=$signs;
        $this->display();
    }
    public function addsigns()
    {
        $signs= D('signs');
        $cid = $_REQUEST['cid'];
        $sign = $_REQUEST['sign'];
       $requst =  $signs->query("INSERT INTO `signs` (`cid`,`sign`) VALUES ('$cid','$sign')");
        if(!$requst){
            $this->success('添加成功！','signlist');die();
        }else{
            $this->error('数据错误');die();
        }

      //  var_dump($_REQUEST);die();

        //$signs= D('signs');
      //  $a=$value = session('class');
    //    $sql="select cid from class where class=$a";
//        $signs->create();
//       if ($signs->create()) {
//           $signs->cid =I($sql);
//           if (false !== $signs->add()) {
//               echo  $signs ->getLastsql();die("test");
//               $this->success('添加成功！','signlist');
//           }
//           else {
//               $this->error('数据错误');
//           }
//       } else {
//           $this->error($signs->getError());
//       }
    }
}